import Vue from 'vue'
import App from './App'
import router from './router'
import './assets/css/global.css'
import './assets/font/iconfont.css'
import axios from 'axios'

import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-chalk/index.css'
import TableTree from 'vue-table-with-tree-grid'

// nprogress相关
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 导入config.js文件
import config from './config.json'

Vue.component('table-tree', TableTree)

Vue.config.productionTip = false
// axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
axios.defaults.baseURL = config.baseURL

axios.interceptors.request.use(function(config) {
  // Do something before request is sent
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})
axios.interceptors.response.use(function(response) {
  // Do something with response data
  NProgress.done()
  return response
})

Vue.prototype.$http = axios

Vue.use(ElementUI)

// 全局前置路由守卫，检测token是否存在
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  var token = window.sessionStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})
